//
//  ESTLoginTool.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/27.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTLoginTool.h"
#import "TYZDeviceInfo.h"
#import "ESTFMDBTool.h"

@implementation ESTLoginTool
/**
 登出

 @param complement 完成回调
 */
+ (void)logoutWithComplement:(void (^)(BOOL))complement {
    [NetTool logoutWithComplement:^(NSString *result) {
        if ([result isEqualToString:Request_Success]) {
            
        } else {
            
        }
        CoreInfo.isLogined = NO;
        CoreInfo.userInfo = nil;
        CoreInfo.auth_token = nil;
        CoreInfo.user_id = nil;
        NetTool.parameter = nil;
        if (complement) {
            complement(YES);
        }
    }];
}

/**
 不下载数据表登录

 @param userAccount 用户名
 @param password 密码
 @param complement 完成回调
 */
+ (void)loginWithUserAccount:(NSString *)userAccount password:(NSString *)password complement:(void (^)(NSString *result))complement {
    [self loginWithUserAccount:userAccount password:password loginSuccess:nil complement:complement];
}

/**
 登录

 @param userAccount 用户名
 @param password 密码
 @param loginSuccess 登录成功回调
 @param complement 最后完成回调
 */
+ (void)loginWithUserAccount:(NSString *)userAccount password:(NSString *)password loginSuccess:(void(^)(NSInteger integer))loginSuccess complement:(void(^)(NSString *result))complement {
    BaseInfo.loginSuccess = loginSuccess;
    BaseInfo.loginComplement = complement;
    WEAKSELF
    [NetTool logginWithUserName:userAccount password:password complement:^(NSDictionary *result) {
        // 一键转模型
        UserInfo = [ESTUserInfo mj_objectWithKeyValues:result];
        NSString *user_type = UserInfo.EMP_INFO[0].SYSTEM_STATION;
        if ([user_type isEqualToString:@"SYS01"]) {
            UserInfo.login_user_type = USER_TYPE_ADMIN;
        } else if ([user_type isEqualToString:@"VE01"]) {
            UserInfo.login_user_type = USER_TYPE_CONSULTANT;
        } else if ([user_type isEqualToString:@"VE02"]) {
            UserInfo.login_user_type = USER_TYPE_SALE_MANAGER;
        } else if ([user_type isEqualToString:@"VE03"]) {
            UserInfo.login_user_type = USER_TYPE_TMK_MANAGER;
        } else if ([user_type isEqualToString:@"VE04"]) {
            UserInfo.login_user_type = USER_TYPE_TMK_COMMISSIONER;
        } else if ([user_type isEqualToString:@"VE05"]) {
            UserInfo.login_user_type = USER_TYPE_MANAGER;
        } else if ([user_type isEqualToString:@"VE06"]) {
            UserInfo.login_user_type = USER_TYPE_INFO_ASSISTANT;
        } else if ([user_type isEqualToString:@"VE07"]) {
            UserInfo.login_user_type = USER_TYPE_ACTIVITY_COMMISSIONER;
        } else if ([user_type isEqualToString:@"VE09"]) {
            UserInfo.login_user_type = USER_TYPE_RECEPTION_COMMISSIONER;
        }
        if (loginSuccess) {
            loginSuccess(0);
#ifdef ACTIVITY
            // 直接登录成功
            BaseInfo.dbCount = 3;
            [weakSelf setSQLCount];
#else
#ifdef DOWNTABLE
            // 下载四个数据表
            [weakSelf configLoginSQL];
#else
            // 直接登录成功
            BaseInfo.dbCount = 3;
            [weakSelf setSQLCount];
#endif
#endif
        }
    }];
}
+ (void)configLoginSQL {
    NSString *licenseId = UserInfo.EMP_INFO[0].LICENSE_ID;
    NSString *prolastModify = [ProvinceFMDB lastModifyWithLicenseId:licenseId];
    NSString *citlastModify = [CityFMDB lastModifyWithLicenseId:licenseId];
    NSString *coulastModify = [CountryFMDB lastModifyWithLicenseId:licenseId];
    NSString *syslastModify = [SysValueFMDB lastModifyWithLicenseId:licenseId];
    [self downloadCityDBDataWithModify:citlastModify];
    [self downloadProvinceDBDataWithModify:prolastModify];
    [self downloadCountryDBDataWithModify:coulastModify];
    [self downloadSysValueDBDataWithModify:syslastModify];
}
+ (NSDictionary *)parameterWithModify:(NSString *)modify {
    return @{
             @"LAST_MODIFY_DATE" : modify,
             @"PAGE_SIZE" : @500,
             @"PAGE_NUM" : @1
             };
}
+ (void)downloadProvinceDBDataWithModify:(NSString *)modify {
    WEAKSELF
    [NetTool postWithCode:Code_Province parameter:[self parameterWithModify:modify] success:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"DATA_INFO"];
        [ProvinceFMDB writeSqlWithDataArray:array];
        [weakSelf setSQLCount];
    } failure:^(NSString *error) {
        BaseInfo.provinceErrorCount ++;
        if (BaseInfo.provinceErrorCount < 3) {
            [weakSelf downloadProvinceDBDataWithModify:modify];
        } else {
            BaseInfo.provinceErrorCount = 0;
            if (BaseInfo.loginComplement) {
                BaseInfo.loginComplement(error);
            }
        }
    }];
}
+ (void)downloadCityDBDataWithModify:(NSString *)modify {
    [NetTool postWithCode:Code_City parameter:[self parameterWithModify:modify] success:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"DATA_INFO"];
        [CityFMDB writeSqlWithDataArray:array];
        [self setSQLCount];
    } failure:^(NSString *error) {
        BaseInfo.cityErrorCount ++;
        if (BaseInfo.cityErrorCount < 3) {
            [self downloadCityDBDataWithModify:modify];
        } else {
            BaseInfo.cityErrorCount = 0;
            if (BaseInfo.loginComplement) {
                BaseInfo.loginComplement(error);
            }
        }
    }];
}
+ (void)downloadCountryDBDataWithModify:(NSString *)modify {
    [NetTool postWithCode:Code_Country parameter:[self parameterWithModify:modify] success:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"DATA_INFO"];
        [CountryFMDB writeSqlWithDataArray:array];
        [self setSQLCount];
    } failure:^(NSString *error) {
        BaseInfo.cityErrorCount ++;
        if (BaseInfo.cityErrorCount < 3) {
            [self downloadCityDBDataWithModify:modify];
        } else {
            BaseInfo.cityErrorCount = 0;
            if (BaseInfo.loginComplement) {
                BaseInfo.loginComplement(error);
            }
        }
    }];
}
+ (void)downloadSysValueDBDataWithModify:(NSString *)modify {
    [NetTool postWithCode:Code_Value_List parameter:[self parameterWithModify:modify] success:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"DATA_INFO"];
        [SysValueFMDB writeSqlWithDataArray:array];
        [self setSQLCount];
    } failure:^(NSString *error) {
        BaseInfo.cityErrorCount ++;
        if (BaseInfo.cityErrorCount < 3) {
            [self downloadCityDBDataWithModify:modify];
        } else {
            BaseInfo.cityErrorCount = 0;
            if (BaseInfo.loginComplement) {
                BaseInfo.loginComplement(error);
            }
        }
    }];
}
+ (void)setSQLCount {
    BaseInfo.dbCount ++;
    if (BaseInfo.loginSuccess) {
        BaseInfo.loginSuccess(BaseInfo.dbCount);
    }
    if (BaseInfo.dbCount == 4) {
        BaseInfo.dbCount = 0;
        if (BaseInfo.loginComplement) {
            BaseInfo.loginComplement(Login_Success);
        }
    }
}
@end
